﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BangleCMS.Model;
using BangleCMS.BLL;

public partial class System_Product_ProductModify : BasePage
{
    #region 属性

    /// <summary>
    /// Id
    /// </summary>
    private int Id
    {
        get
        {
            return ViewState["Id"] == null ? 0 : Convert.ToInt32(ViewState["Id"]);
        }
        set
        {
            ViewState["Id"] = value;
        }
    }

    /// <summary>
    /// Action
    /// </summary>
    private string Action
    {
        get
        {
            return ViewState["Action"] == null ? "Add" : ViewState["Action"].ToString();
        }
        set
        {
            ViewState["Action"] = value;
        }
    }

    #endregion

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        //检验用户权限
        //base.CheckUserPermission("001001");

        if (!IsPostBack)
        {
            Id = base.ParseQueryInt("Id", 0);
            Action = base.ParseQueryString("Action", "Add");

            getDate(Id);
        }
    }
    #endregion

    #region 加载数据
    private void getDate(int id)
    {
        if (Action == "Update")
        {
            t_product model = new t_product();
            t_productBLL bll = new t_productBLL();
            model = bll.GetModel(id);

            this.txt_Title.Text = model.Title;
            //this.txt_ProductType.Text = model.ProductType;
            this.txt_Abstract.Text = model.Abstract;
            this.txt_Keywords.Text = model.Keywords;
            this.txt_Description.Text = model.Description;
            this.radio_Valid.SelectedValue = model.Valid.ToString();
            this.Editor.Text = model.Content;
            model.ReadCount = 0;

            //this.img_Product.ImageUrl = model.LogoImg;
            //this.hf_LogoImg.Value = model.LogoImg;
            this.txt_ReleaseDate.Text = model.ReleaseDate.GetValueOrDefault().ToString("yyyy-MM-dd");
        }
        else
        {
            this.txt_ReleaseDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }
    }
    #endregion

    #region 提交
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        //if (this.hf_LogoImg.Value.Length <= 0)
        //{
        //    base.Alert("还没上传产品图片！");
        //    return;
        //}

        if (Action == "Update")
        {
            #region 修改
            try
            {
                t_product model = new t_product();
                t_productBLL bll = new t_productBLL();

                model = bll.GetModel(Id);

                if (model == null)
                {
                    base.Alert("操作失败！");
                    return;
                }

                model.Title = this.txt_Title.Text.Trim();
                //model.ProductType = this.txt_ProductType.Text.Trim();
                model.Abstract = this.txt_Abstract.Text.Trim();
                model.Keywords = this.txt_Keywords.Text.Trim();
                model.Description = this.txt_Description.Text.Trim();
                model.Valid = Convert.ToInt32(this.radio_Valid.SelectedValue);
                model.Content = this.Editor.Text;
                model.ReadCount = 0;
                //model.LogoImg = this.hf_LogoImg.Value;
                model.ReleaseDate = Convert.ToDateTime(this.txt_ReleaseDate.Text.Trim());

                model.CreateUser = base.CurrentSession.UserID;
                model.CreateDate = DateTime.Now;
                model.UpdateUser = model.CreateUser;
                model.UpdateDate = model.CreateDate;

                bll.Update(model);

                base.Redirect("操作成功！", "ProductList.aspx");
            }
            catch (Exception ex)
            {
                base.Alert("操作失败！");
                //throw ex;
            }
            #endregion
        }
        else
        {
            #region 新增
            try
            {
                t_product model = new t_product();
                t_productBLL bll = new t_productBLL();

                model.Title = this.txt_Title.Text.Trim();
                //model.ProductType = this.txt_ProductType.Text.Trim();
                model.Abstract = this.txt_Abstract.Text.Trim();
                model.Keywords = this.txt_Keywords.Text.Trim();
                model.Description = this.txt_Description.Text.Trim();
                model.Valid = Convert.ToInt32(this.radio_Valid.SelectedValue);
                model.Content = this.Editor.Text;
                model.ReadCount = 0;
                //model.LogoImg = this.hf_LogoImg.Value;
                model.ReleaseDate = Convert.ToDateTime(this.txt_ReleaseDate.Text.Trim());

                model.CreateUser = base.CurrentSession.UserID;
                model.CreateDate = DateTime.Now;
                model.UpdateUser = model.CreateUser;
                model.UpdateDate = model.CreateDate;
                bll.Add(model);

                base.Redirect("操作成功！", "ProductList.aspx");
            }
            catch (Exception ex)
            {
                base.Alert("操作失败！");
                //throw ex;
            }
            #endregion
        }

    }
    #endregion

}
